#include "ordercard.h"
#include "ui_ordercard.h"
#include "foodinordercard.h"
#include <QWidget>
#include "systemdatabase.h"
#include <QDateTime>

OrderCard::OrderCard(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::OrderCard)
{
    ui->setupUi(this);
    SystemDataBase db;

    // 设置订单包含食物卡片
    // 查询订单号
    QString id_user = tr("user13828081666");
    QSqlQuery query;
    query.prepare("select orderid from orders where id_user = :a");
    query.bindValue(0,id_user);
    query.exec();
    query.next();
    QString id_order = query.value(0).toString();

    // 查询餐厅id
    QString id_DR;
    query.prepare("select id_DR from orders where id_order = :a");
    query.bindValue(0,id_order);
    query.exec();
    query.next();
    id_DR = query.value(0).toString();

    // 查询菜品id
    // QString id_food;
    query.prepare("select id_food from ordertofood where id_order = :a");
    query.bindValue(0,id_order);
    query.exec();

    int x = parent->x();
    int y = ui->horizontalLayoutWidget->y() + ui->horizontalLayoutWidget->height();
    for(int i = 0 ; query.next() ; i++){

        // 设置食物卡片位置和尺寸
        FoodInOrderCard *card = new FoodInOrderCard(this);
        card->resize(parent->width(),150);
        card->move(x,y+i*150);

        // 设置卡片内容，并计算订单总金额
        sum += card->setText(query.value(0).toString());
    }
    setText();
}

// 设置订单卡片内容
void OrderCard::setText(){   

    // 查询订单号
    QString id_user = tr("user13828081666");
    QSqlQuery query;
    query.prepare("select orderid from orders where id_user = :a");
    query.bindValue(0,id_user);
    query.exec();
    query.next();
    QString id_order = query.value(0).toString();

    this->ui->orderCode->setText(id_order);

    // 查询订单创建时间
    query.prepare("select OrderCreateTime from orders where orderid = :a");
    query.bindValue(0,id_order);
    query.exec();
    query.next();
    QDateTime time = query.value(0).toDateTime();
    QString dt = time.toString("yyyy-MM-dd hh:mm:ss");

    this->ui->datetime->setText(dt);

    // 设置订单总金额
    // QString SumText =
    // this->ui->orderPayment->setText();

}

OrderCard::~OrderCard()
{
    delete ui;
}
